{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 量子近似优化算法\n",
    "\n",
    "\n",
    "`Linux` `CPU` `全流程` `初级` `中级` `高级`\n",
    "\n",
    "[![](https://gitee.com/mindspore/docs/raw/master/tutorials/training/source_zh_cn/_static/logo_source.png)](https://gitee.com/mindspore/mindquantum/blob/master/tutorials/quantum_approximate_optimization_algorithm.ipynb)\n",
    "\n",
    "## 概述\n",
    "\n",
    "量子近似优化算法（Quantum Approximate Optimization Algorithm，QAOA）是利用量子计算机来近似解决组合优化问题的量子算法，最早由Farhi等人于2014年提出。在本教程里，我们将利用QAOA算法来解决最大割问题（Max-Cut），来熟悉MindQuantum中量子线路的搭建和训练。\n",
    "\n",
    "> 本文档适用于CPU环境。   \n",
    "> 你可以在这里找到完整的可运行的样例代码：<https://gitee.com/mindspore/mindquantum/blob/master/tutorials/source/quantum_approximate_optimization_algorithm.py>。\n",
    "\n",
    "## 环境准备\n",
    "\n",
    "本教程所需要的额外库：\n",
    "\n",
    "- networkx\n",
    "\n",
    "> `networkx`是创建、操作和研究复杂网络的结构、动态和功能库。可通过`pip3 install networkx`来进行安装。\n",
    "\n",
    "## Max-Cut问题描述\n",
    "\n",
    "Max-Cut问题是图论中的一个NP-complete问题，它需要将一个图中的顶点分成两部分，并使得两部分被切割的边最多。如下图（a），一个图由五个顶点构成，相互连接的边为```(0, 1), (0, 2), (1, 2), (2, 3), (3, 4), (0, 4)```。为了使得被切割的边最多，我们尝试通过（b）图的分割，将1、2、4分为一组，0、3分成另一组，因此可得到被切割的边有5条。当图中顶点增多时，我们很难找到有效的经典算法来解决Max-Cut问题。下面，我们介绍怎么将Max-Cut问题转化为一个哈密顿量的基态能力求解问题。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![max cut](./images/Max_Cut.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Max-Cut问题量子化\n",
    "\n",
    "这里我们将图中的每个顶点赋予一个量子比特，当顶点被分到左边时，我们将该顶点上的量子比特设置为$\\left|0\\right>$态，同理，右边为$\\left|1\\right>$态，当两个顶点被分到不同的集合中时，这两个顶点上的比特将处于不同的量子态。例如对于第0个顶点和第1个顶点，当其连线被切割是，两个顶点上的比特对应的量子态可以为$\\left|\\psi\\right>=\\left|0_11_0\\right>$或$\\left|\\psi\\right>=\\left|1_10_0\\right>$，其中下角标表示顶点的序号。此时，我们选择哈密顿量$H=(Z_1Z_0-1)/2$，这里$Z$为泡利$Z$算符。不难发现：\n",
    "$$\\left<\\psi\\right|H\\left|\\psi\\right>=-1$$\n",
    "而当顶点被分到同一集合中是，不难验证此时：\n",
    "$$\\left<\\psi\\right|H\\left|\\psi\\right>=0$$\n",
    "因此，我们只用按照上面的规则，写出图对应的哈密顿量$H$，利用量子计算机求得$H$的基态能量与基态，我们就可以得到该图的Max-Cut切割方案与最大切割边数。我们记所有边的集合为$C$，所有边个数为$c$，则哈密顿量可写为：\n",
    "$$H=\\sum_{(i,j)\\in C}(Z_iZ_j-1)/2$$\n",
    "\n",
    "## 导入相关依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
   ],
   "source": [
    "from mindquantum import Circuit, Hamiltonian, UN, H, ZZ, RX, StateEvolution\n",
    "from mindquantum.nn import MindQuantumAnsatzOnlyLayer\n",
    "from mindquantum.ops import QubitOperator\n",
    "import networkx as nx\n",
    "import mindspore.nn as nn\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 搭建所需求解的图\n",
    "\n",
    "通过`add_path`可在图中添加边。最后画出图的结构。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAb4AAAEuCAYAAADx63eqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24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2AnIiJSHN7cQkREfoXBR0REfoXBR0REfoXBR0REfoXBR0REfoXBR0REfoXBR0REfoXBR0REfoXBR0REfuX/AW4Dq0Lmn5ChAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "g = nx.Graph()\n",
    "nx.add_path(g, [0,1])\n",
    "nx.add_path(g, [1,2])\n",
    "nx.add_path(g, [2,3])\n",
    "nx.add_path(g, [3,4])\n",
    "nx.add_path(g, [0,4])\n",
    "nx.add_path(g, [0,2])\n",
    "nx.draw(g,with_labels=True, font_weight='bold')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如上如，我们得到一个由5个节点和6条边构成的图结构。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 搭建QAOA量子线路\n",
    "\n",
    "### 线路搭建\n",
    "\n",
    "这里我们采用量子绝热近似算法，经过演化将量子态从$X^{\\otimes n}$的本征态演化到图多应哈密的量的基态。\n",
    "\n",
    "搭建图对应哈密顿量的含时演化线路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_hc(g,para):\n",
    "    hc = Circuit()\n",
    "    for i in g.edges:\n",
    "        hc += ZZ(para).on(i)\n",
    "    return hc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "搭建$X^{\\otimes n}$含时演化的量子线路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def build_hb(g, para):\n",
    "    hc = Circuit()\n",
    "    for i in g.nodes:\n",
    "        hc += RX(para).on(i)\n",
    "    return hc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为了使得最后优化的结果足够准确，我们需要将量子线路重复多次，因此我们通过如下函数搭建多层的训练网络："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_ansatz(g, p):\n",
    "    c = Circuit()\n",
    "    for i in range(p):\n",
    "        c += build_hc(g,f'g{i}')\n",
    "        c += build_hb(g,f'b{i}')\n",
    "    return c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构建图对应的哈密顿量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def build_ham(g):\n",
    "    hc = QubitOperator()\n",
    "    for i in g.edges:\n",
    "        hc += QubitOperator(f'Z{i[0]} Z{i[1]}')\n",
    "    return hc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成完整的量子线路和图所对应的哈密顿量\n",
    "\n",
    "这里我们选择`p = 4`，表示选用4曾的QAOA量子线路，`ansatz`是求解该问题的量子线路，`init_state_circ`是将量子态制备到均匀叠加态上的量子线路。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = 4\n",
    "ham = Hamiltonian(build_ham(g))\n",
    "ansatz = build_ansatz(g, p)\n",
    "init_state_circ = UN(H, g.nodes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 搭建待训练量子神经网络\n",
    "\n",
    "由于该问题不需要编码层量子线路，我们这里使用`MindQuantumAnsatzOnlyLayer`作为待训练的量子神经网络，并采用`Adam`优化器。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = MindQuantumAnsatzOnlyLayer(ansatz.para_name, init_state_circ+ansatz, ham)\n",
    "opti = nn.Adam(net.trainable_params(), learning_rate=0.05)\n",
    "train_net = nn.TrainOneStepCell(net, opti)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 训练并展示结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train step: 0 , cut: [[3.0059216]]\n",
      "train step: 10 , cut: [[3.3262742]]\n",
      "train step: 20 , cut: [[3.7228582]]\n",
      "train step: 30 , cut: [[3.983411]]\n",
      "train step: 40 , cut: [[4.135832]]\n",
      "train step: 50 , cut: [[4.216693]]\n",
      "train step: 60 , cut: [[4.2141833]]\n",
      "train step: 70 , cut: [[4.2036085]]\n",
      "train step: 80 , cut: [[4.260594]]\n",
      "train step: 90 , cut: [[4.373112]]\n",
      "train step: 100 , cut: [[4.4853263]]\n",
      "train step: 110 , cut: [[4.5553446]]\n",
      "train step: 120 , cut: [[4.587566]]\n",
      "train step: 130 , cut: [[4.611128]]\n",
      "train step: 140 , cut: [[4.637698]]\n",
      "train step: 150 , cut: [[4.6584387]]\n",
      "train step: 160 , cut: [[4.66508]]\n",
      "train step: 170 , cut: [[4.663408]]\n",
      "train step: 180 , cut: [[4.6678705]]\n",
      "train step: 190 , cut: [[4.6875486]]\n",
      "train step: 200 , cut: [[4.7206187]]\n",
      "train step: 210 , cut: [[4.7580614]]\n",
      "train step: 220 , cut: [[4.7893686]]\n",
      "train step: 230 , cut: [[4.8074245]]\n",
      "train step: 240 , cut: [[4.8116426]]\n",
      "train step: 250 , cut: [[4.8077316]]\n",
      "train step: 260 , cut: [[4.803544]]\n",
      "train step: 270 , cut: [[4.8039436]]\n",
      "train step: 280 , cut: [[4.8088512]]\n",
      "train step: 290 , cut: [[4.8154163]]\n",
      "train step: 300 , cut: [[4.821649]]\n",
      "train step: 310 , cut: [[4.8281393]]\n",
      "train step: 320 , cut: [[4.8366113]]\n",
      "train step: 330 , cut: [[4.847317]]\n",
      "train step: 340 , cut: [[4.858108]]\n",
      "train step: 350 , cut: [[4.865946]]\n",
      "train step: 360 , cut: [[4.8693476]]\n",
      "train step: 370 , cut: [[4.869488]]\n",
      "train step: 380 , cut: [[4.868954]]\n",
      "train step: 390 , cut: [[4.8695197]]\n",
      "train step: 400 , cut: [[4.8711824]]\n",
      "train step: 410 , cut: [[4.8730283]]\n",
      "train step: 420 , cut: [[4.874686]]\n",
      "train step: 430 , cut: [[4.8768916]]\n",
      "train step: 440 , cut: [[4.880748]]\n",
      "train step: 450 , cut: [[4.8865013]]\n",
      "train step: 460 , cut: [[4.8930907]]\n",
      "train step: 470 , cut: [[4.898922]]\n",
      "train step: 480 , cut: [[4.9031305]]\n",
      "train step: 490 , cut: [[4.906122]]\n",
      "train step: 500 , cut: [[4.9088955]]\n",
      "train step: 510 , cut: [[4.9119415]]\n",
      "train step: 520 , cut: [[4.9149566]]\n",
      "train step: 530 , cut: [[4.9175825]]\n",
      "train step: 540 , cut: [[4.920064]]\n",
      "train step: 550 , cut: [[4.9228735]]\n",
      "train step: 560 , cut: [[4.925872]]\n",
      "train step: 570 , cut: [[4.9282985]]\n",
      "train step: 580 , cut: [[4.929679]]\n",
      "train step: 590 , cut: [[4.930426]]\n"
     ]
    }
   ],
   "source": [
    "for i in range(600):\n",
    "    if i%10 == 0:\n",
    "        print(\"train step:\", i, \", cut:\", (len(g.edges)-train_net())/2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据上面的训练结果我们发现，该问题哈密顿量的基态能量对应的边切割数趋近与5。\n",
    "\n",
    "### 量子态展示\n",
    "\n",
    "前面我们通过训练得到了量子线路中参数的最优值，下面，我们通过`StateEvolution`类的`final_state`来输出量子线路在最优参数时的量子态，其中`ket`参数表示是否将最终量子态表示为右矢形式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.017737679183483124-0.03180303797125816j)¦00000⟩\n",
      "(-0.02683155983686447+0.0012889178469777107j)¦00001⟩\n",
      "(0.011993971653282642+0.006973826792091131j)¦00010⟩\n",
      "(-0.014608755707740784-0.003942559473216534j)¦00011⟩\n",
      "(-0.02683155983686447+0.0012889178469777107j)¦00100⟩\n",
      "(0.00725862430408597+0.10942266136407852j)¦00101⟩\n",
      "(-0.014608755707740784-0.003942559473216534j)¦00110⟩\n",
      "(0.008969870395958424-0.004171415697783232j)¦00111⟩\n",
      "(0.00950924027711153-0.00026544960564933717j)¦01000⟩\n",
      "(-0.37196943163871765-0.3156493902206421j)¦01001⟩\n",
      "(-0.040885526686906815+0.037214867770671844j)¦01010⟩\n",
      "(-0.37196943163871765-0.3156493902206421j)¦01011⟩\n",
      "(-0.03160367161035538+0.009305878542363644j)¦01100⟩\n",
      "(-0.040885526686906815+0.037214867770671844j)¦01101⟩\n",
      "(-0.03160367161035538+0.009305878542363644j)¦01110⟩\n",
      "(0.00950924027711153-0.00026544960564933717j)¦01111⟩\n",
      "(0.00950924027711153-0.00026544960564933717j)¦10000⟩\n",
      "(-0.03160367161035538+0.009305878542363644j)¦10001⟩\n",
      "(-0.040885526686906815+0.037214867770671844j)¦10010⟩\n",
      "(-0.03160367161035538+0.009305878542363644j)¦10011⟩\n",
      "(-0.37196943163871765-0.3156493902206421j)¦10100⟩\n",
      "(-0.040885526686906815+0.037214867770671844j)¦10101⟩\n",
      "(-0.37196943163871765-0.3156493902206421j)¦10110⟩\n",
      "(0.00950924027711153-0.00026544960564933717j)¦10111⟩\n",
      "(0.008969870395958424-0.004171415697783232j)¦11000⟩\n",
      "(-0.014608755707740784-0.003942559473216534j)¦11001⟩\n",
      "(0.00725862430408597+0.10942266136407852j)¦11010⟩\n",
      "(-0.02683155983686447+0.0012889178469777107j)¦11011⟩\n",
      "(-0.014608755707740784-0.003942559473216534j)¦11100⟩\n",
      "(0.011993971653282642+0.006973826792091131j)¦11101⟩\n",
      "(-0.02683155983686447+0.0012889178469777107j)¦11110⟩\n",
      "(0.017737679183483124-0.03180303797125816j)¦11111⟩\n"
     ]
    }
   ],
   "source": [
    "pr = dict(zip(ansatz.para_name, net.weight.asnumpy()))\n",
    "print(StateEvolution(init_state_circ+ansatz).final_state(pr, ket=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 概率图\n",
    "\n",
    "我们画出最终量子态在计算基矢下的概率分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEMCAYAAADK231MAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAnBklEQVR4nO2dedxmc/nH39csdibMFNlGTDIYZIztlyXLDBOjiBEa69hVIkKWkSJLq7JkEtFYKk0ZpCwVYUZJ6EfDTwz9aiz9Kmvy/f1xXbf7+5y5l/PMcz+L4/N+ve7Xc59zrvv6Xmf7XN/tnMdSSgghhKgug/o7ACGEEL2LhF4IISqOhF4IISqOhF4IISqOhF4IISqOhF4IISrOkP4OoMjw4cPTyJEj+zsMIYR4S3Hfffc9m1Ia0WjbgBP6kSNHMmfOnP4OQwgh3lKY2Z+bbVPXjRBCVBwJvRBCVBwJvRBCVBwJvRBCVBwJvRBCVBwJvRBCVBwJvRBCVBwJvRBCVJwB98CUGJiMPOGGptueOGtix3wtjD/hdPK46hxVC9XohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4kjohRCi4pQSejObYGaPmNlcMzuhwfZjzOxhM3vAzH5hZqtl26aY2Z/iM6WTwQshhGhPW6E3s8HABcCOwGhgLzMbXTD7HTA2pTQGuA74Uvx2OeBUYBNgHHCqmS3bufCFEEK0o0yNfhwwN6X0eErpNWAGMCk3SCndllJ6KRbvBlaO7+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p/wvUymieD0+IcfbaF3VF4C++ZBmV111d/lFn09b3MJv+fArWEcXr4WsAOf0L9eepJdqF99UeZ3fB1exyvXh14baif/S3gpQP1Jx6PxGcabJmtv4Ou86Hb2nXDV5mksVIHfXUyUQ1vsZ+3Ef8Jp0l5eTLYA+8rL2O3/wD1VdouO0bL468n+H4sr4MPbtdeItcsGRyGz/bapoTdVXhLohO++qPMoq83BS7s3ouPH21X8NXI7g/4eEwnfPVHmWV9rd8v+tkfhS50sD4YdQReO56Ki9tDZE/IlrUraVM2IXTSV8cSVZnYWvh6MxmUtRuovrpjVzh2w/GHbx7BxztWLmxvmAwa+Glr10lf/VFmZvO9WN6AeHq5u3ad9NUfZZb11ZeffhfvbgfsD69sg09tuoyCsHXHrqRN2eTSSV8dSVRlY+uPuPraV3ePWfabT+FjHw2b27RJBt2x66Sv/igzs/lvfK75u9v4amrXSV/9UWZZX3316beCexy4N70HdcKunQ0lk0snfZWx605c7WLrj7j62tdCnMtl8SewW757hDbJoDt2nfTVH2UOVF8DOf6++PRr4W+1Tyuh7C1fZeze6nH1ta9u2i3WZnvZZNDWrpO++qPMgeprIMffV5+35LtuhBhImNliKaVXOmHXSV/9UeZA9dUfZZb11RdI6IUQouIM6u8AhBBC9C4SeiGEqDgSeiGEqDgSeiGEqDgSeiGEqDgSeiGEqDj/D/E1Aytbu74LAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def show_amp(state):\n",
    "    amp = np.abs(state)**2\n",
    "    n_qubits = int(np.log2(len(amp)))\n",
    "    labels = [bin(i)[2:].zfill(n_qubits) for i in range(len(amp))]\n",
    "    plt.bar(labels, amp)\n",
    "    plt.xticks(rotation=45)\n",
    "    plt.show()\n",
    "state = StateEvolution(init_state_circ+ansatz).final_state(pr)\n",
    "show_amp(state)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据概率分布图我们发现，该Max-Cut问题具有四个简并解，每个解对应的概率大概为25%。\n",
    "\n",
    "## 总结\n",
    "\n",
    "这里我们通过量子近似优化算法来解决了Max-Cut问题，并得到了案例中的图对应的最大切割方案。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参考文献\n",
    "\n",
    "[1] Edward Farhi, Jeffrey Goldstone, and Sam Gutmann. [A Quantum Approximate Optimization Algorithm](https://arxiv.org/pdf/1411.4028.pdf)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "5545a57ef4a1ac7dca167cae0bf17fda051fcd0639773c034bd7ce77ffd97d30"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
